<?php
class StudentsController extends AppController {

	var $name = 'Students';

	function index() {
		$this->Student->recursive = 0;
		$this->set('students', $this->paginate());
	}

	/**
	 * Método usado como index do site.
	 * @author Fellipe Férrer < fellipe.tenorio@gmail.com >
	 */
	function home(){
		//$this->Layout->$title_for_layout = "Estudantes";
		$this->pageTitle = "Home";
		$this->loadModel("Opportunity");
		$this->set("Opportunities", $this->Opportunity->find("list"));
		$opportunities = $this->Opportunity->find('all');
		//pr($opportunities);
	}
	
	/**
	 * Método usado para criar o Student associado ao User
	 * @author Fellipe Férrer < fellipe.tenorio@gmail.com >
	 */
	function beggin(){
		$this->_onlyToLogged();
		if(!empty($this->data)){
			$stdData = $this->data['Student'];
			if(empty($stdData['cpf']) ||	empty($stdData['name']) || empty($stdData['rg']) ||
				empty($stdData['curso']) ||	empty($stdData['entrada']) || empty($stdData['endereco']) ||
				empty($stdData['bairro']) ||	empty($stdData['cidade']) || empty($stdData['cep']) ||	
				empty($stdData['email'])){
					$this->Session->setFlash(__('Please enter all inputs!', true));
				} else {
					$stdData['uid'] = $this->Session->Read('user.id');
					if ($this->Student->save($stdData)) {
						$this->Session->setFlash(__('The student has been created!', true));
						$this->redirect(array('controller'=>'internships', 'action'=>'beggin'));
					}
				}
		}
	}
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid student', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('student', $this->Student->read(null, $id));
	}

	function add() {
		if($this->Session->Read('user.level') != "")
			$this->redirect(array('controller'=>'users', 'action'=>'home'));
		if (!empty($this->data)) {
			$this->Student->create();
			if ($this->Student->save($this->data)) {
				$this->Session->setFlash(__('The student has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The student could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid student', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Student->save($this->data)) {
				$this->Session->setFlash(__('The student has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The student could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Student->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Id inválido', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Student->delete($id)) {
			$this->Session->setFlash(__('Student deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Student was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function login(){
		if(!empty($this->data)){
			if(empty($this->data['Student']['cpf']) || empty($this->data['Student']['password'])){
				$this->Session->setFlash('Preencha todos os campos!');
			} else {
				$std = $this->Student->find('all', array('conditions' => array('cpf' => $this->data['Student']['cpf'], 'password' => $this->data['Student']['password'])));
				if(count($std)){
					$this->Session->write('usuario.login', $this->data['Student']['cpf']);
				} else {
					$this->Session->setFlash(__("User doen't exist!", true));
				}
			}
		}
	}
	
	function logout(){
		$this->Session->delete('usuario');
		$this->redirect(array('action' => 'home'));
	}
	private function _onlyToAdmin(){
		$this->_onlyToLogged();
		if($this->Session->Read('user.level') != '1')
			$this->redirect(array('controller'=>'users', 'action'=>'home'));
	}
	
	private function _onlyToLogged(){
		if($this->Session->Read('user.email') == '')
			$this->redirect(array('controller'=>'users', 'action'=>'home'));
	}
	
	private function _isAdmin(){
		return $this->Session->Read('user.level') == '1'?true:false;
	}
}
?>